//
//  WarnListViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/8/6.
//

#import "WarnListViewController.h"
#import "joinQuestListModel.h"
#import "joinScreenViewController.h"
#import "WarnListTableViewCell.h"
#import "TimeFrameShowView.h"
#import "joinQuestDetailsViewController.h"
#import "ExamineModel.h"
#import "joinScreenModel.h"
#import "WarnModel.h"
#import "WarnDetailsViewController.h"

@interface WarnListViewController ()

///全部
@property (nonatomic ,strong) UIButton * qbBtn;
///转
@property (nonatomic ,strong) UIButton * zjBtn;
///拒接
@property (nonatomic ,strong) UIButton * jsBtn;
@property (nonatomic ,strong) UIView * btnline;

///筛选
@property (nonatomic ,strong) UIButton * SXBtn;

@property (nonatomic ,strong) UIView * sxview;
@property (nonatomic ,strong) UIButton * sxqbBtn;
@property (nonatomic ,strong) UIButton * sxycBtn;
@property (nonatomic ,strong) UIButton * sxjjBtn;
@property (nonatomic ,strong) UILabel * sumLab;

@property (nonatomic ,strong) NSMutableArray <WarnModel *> * modelArray;


@property (nonatomic ,strong) joinScreenModel * model;

///获取数量
@property (nonatomic ,strong) NSURLSessionDataTask * task;
@end

@implementation WarnListViewController
-(NSMutableArray<WarnModel *> *)modelArray{
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
- (joinScreenModel *)model{
    if(!_model){
        _model = [joinScreenModel new];
        _model.status = -1;
        _model.alarmDegree = -1;
        if([self.alarmDegree isEqualToString:@"0"]){
            _model.alarmDegree = 0;
        }
        if([self.alarmDegree isEqualToString:@"1"]){
            _model.alarmDegree = 1;
        }
    }
    return _model;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self refreshingData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.shutDownBack = YES;
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"告警记录" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    
    
    UIView * btnview = [UIView new];
    [self.view addSubview:btnview];
    btnview.backgroundColor = KcolorHex(@"#F1F5FA");
    [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topview.mas_bottom);
        make.left.right.offset(0);
        make.height.offset(kwt(50));
    }];
    
    UIButton * qbbtn = [UIButton buttonWithType:0];
    self.qbBtn = qbbtn;
    qbbtn.addTarget(self,@selector(qbBtnClick));
    qbbtn.addNormalText(@"全部").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [btnview addSubview:qbbtn];
    [qbbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.offset(0);
        make.width.offset(kwt(80));
    }];
    
    UIButton * zjBtn = [UIButton buttonWithType:0];
    self.zjBtn = zjBtn;
    zjBtn.addTarget(self,@selector(zjBtnClick));
    zjBtn.addNormalText(@"待处理").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [btnview addSubview:zjBtn];
    [zjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.offset(0);
        make.left.equalTo(qbbtn.mas_right);
        make.width.offset(kwt(80));
    }];
    
    
    UILabel * sumLab = [DTKTool labelWithText:@"0" andFont:kCFont(10) andTextColor:UIColor.whiteColor];
    [zjBtn addSubview:sumLab];
    sumLab.textAlignment = NSTextAlignmentCenter;
    self.sumLab = sumLab;
    sumLab.hidden = YES;
    [DTKTool YJ:kwt(9) andView:sumLab];
    sumLab.backgroundColor = KcolorHex(@"#FE5402");
    [sumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(kwt(18));
        make.centerX.equalTo(zjBtn.mas_right).offset(kwt(-6));
        make.centerY.equalTo(zjBtn.mas_top).offset(kwt(12));
    }];
    UIButton * jsBtn = [UIButton buttonWithType:0];
    self.jsBtn = jsBtn;
    jsBtn.addTarget(self,@selector(jsBtnClick));
    jsBtn.addNormalText(@"已处理").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [btnview addSubview:jsBtn];
    [jsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.top.offset(0);
        make.left.equalTo(zjBtn.mas_right);
        make.width.offset(kwt(80));
    }];
 
    
    UIView * btnline = [UIView new];
    self.btnline = btnline;
    btnline.backgroundColor = KcolorHex(@"#4965FE");
    btnline.frame = CGRectMake(kwt(27), kwt(47), kwt(26), kwt(3));
    [btnview addSubview:btnline];
    
    UIButton * SXBtn = [UIButton buttonWithType:0];
    self.SXBtn = SXBtn;
    if(self.alarmDegree.length > 0){
        self.SXBtn.selected = YES;
    }
    SXBtn.addTarget(self,@selector(SXBtnClick));
    SXBtn.addNormalText(@"筛选").addNormalColor(KcolorHex(@"#222222")).addFont(kFont(17));
    [SXBtn setTitleColor:KcolorHex(@"#0967FF") forState:UIControlStateSelected];
    [SXBtn setImage:[UIImage imageNamed:@"CRscreen"] forState:UIControlStateNormal];
    [SXBtn setImage:[UIImage imageNamed:@"CRscreen2"] forState:UIControlStateSelected];
    [SXBtn setImageEdgeInsets:UIEdgeInsetsMake(0, kwt(50), 0, 0)];
    [SXBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, kwt(-40), 0, 0)];
    [btnview addSubview:SXBtn];
    [SXBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.top.offset(0);
        make.width.offset(kwt(80));
    }];
    UIView * ssline = [UIView new];
    ssline.backgroundColor = KcolorHex(@"#C4C4C4");
    [btnview addSubview:ssline];
    [ssline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.centerY.offset(0);
        make.width.offset(0.5);
        make.height.offset(kwt(14));
    }];
    
    UIView * line = [UIView new];
    line.backgroundColor = KcolorHex(@"#D8D8D8");
    [btnview addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.offset(0);
        make.height.offset(0.5);
    }];
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#ECEDEE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(btnview.mas_bottom);
    }];
    
    [self addFooterView:^{
        [weakSelf addMoreData];
    }];
    [self addHeaderView:^{
        [weakSelf refreshingData];
    }];
    
}
- (void)SXBtnClick{
    self.sxview.hidden = !self.sxview.hidden;
}
- (UIView *)sxview{
    if(!_sxview){
        
        _sxview = [UIView new];
        self.sxview.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [self.view addSubview:_sxview];
        [_sxview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.left.bottom.equalTo(self.tableview);
        }];
        self.sxview.hidden = YES;
        UIView * backview = [UIView new];
        backview.backgroundColor = UIColor.whiteColor;
        [_sxview addSubview:backview];
        [backview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.right.left.offset(0);
            make.height.offset(kwt(174));
        }];
    
        UIButton * sxqbBtn = [UIButton buttonWithType:0];
        sxqbBtn.addNormalText(@"全部").addNormalColor(KcolorHex(@"#242729")).addTarget(self,@selector(sxqbBtnClick)).addFont(kCFont(16));
        self.sxqbBtn = sxqbBtn;
        [sxqbBtn setTitleColor:KcolorHex(@"#0669FE") forState:UIControlStateSelected];
        [backview addSubview:sxqbBtn];
        [sxqbBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.offset(0);
        }];
        
        UIView * line = [UIView new];
        line.backgroundColor = KcolorHex(@"#DEE0E2");
        [backview addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(kwt(10));
            make.right.offset(kwt(-10));
            make.top.equalTo(sxqbBtn.mas_bottom);
            make.height.offset(0.5);
        }];
        
        
        UIButton * sxycBtn = [UIButton buttonWithType:0];
        sxycBtn.addNormalText(@"异常告警").addNormalColor(KcolorHex(@"#242729")).addTarget(self,@selector(sxycBtnClick)).addFont(kCFont(16));
        self.sxycBtn = sxycBtn;
        [sxycBtn setTitleColor:KcolorHex(@"#0669FE") forState:UIControlStateSelected];
        [backview addSubview:sxycBtn];
        [sxycBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            make.top.equalTo(sxqbBtn.mas_bottom);
            make.height.equalTo(sxqbBtn);
        }];
        
        UIView * line1 = [UIView new];
        line1.backgroundColor = KcolorHex(@"#DEE0E2");
        [backview addSubview:line1];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(kwt(10));
            make.right.offset(kwt(-10));
            make.top.equalTo(sxycBtn.mas_bottom);
            make.height.offset(0.5);
        }];
        
        UIButton * sxjjBtn = [UIButton buttonWithType:0];
        sxjjBtn.addNormalText(@"紧急告警").addNormalColor(KcolorHex(@"#242729")).addTarget(self,@selector(sxjjBtnClick)).addFont(kCFont(16));
        self.sxjjBtn = sxjjBtn;
        [sxjjBtn setTitleColor:KcolorHex(@"#0669FE") forState:UIControlStateSelected];
        [backview addSubview:sxjjBtn];
        [sxjjBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.offset(0);
            make.top.equalTo(sxycBtn.mas_bottom);
            make.height.equalTo(sxycBtn);
        }];
        
        if(self.model.alarmDegree == -1){
            sxqbBtn.selected = YES;
        }
        if(self.model.alarmDegree == 0){
            sxjjBtn.selected = YES;
        }
        if(self.model.alarmDegree == 1){
            sxycBtn.selected = YES;
        }
    }
    return _sxview;
}
- (void)sxqbBtnClick{
    self.SXBtn.selected = NO;
    self.sxqbBtn.selected = YES;
    self.sxjjBtn.selected = NO;
    self.sxycBtn.selected = NO;
    self.model.alarmDegree = -1;
    self.sxview.hidden = YES;
    [self refreshingData];
}

- (void)sxjjBtnClick{
    
    self.SXBtn.selected = YES;
    self.sxqbBtn.selected = NO;
    self.sxjjBtn.selected = YES;
    self.sxycBtn.selected = NO;
    self.model.alarmDegree = 0;
    self.sxview.hidden = YES;
    [self refreshingData];

}

- (void)sxycBtnClick{
    
    self.SXBtn.selected = YES;
    self.sxqbBtn.selected = NO;
    self.sxjjBtn.selected = NO;
    self.sxycBtn.selected = YES;
    self.model.alarmDegree = 1;
    self.sxview.hidden = YES;
    [self refreshingData];

}
- (void)btnClickWithbtn:(UIButton *)btn{
    self.qbBtn.addFont(kFont(17));
    self.zjBtn.addFont(kFont(17));
    self.jsBtn.addFont(kFont(17));
    btn.addFont(kCFont(19));
    self.btnline.mj_x = btn.mj_x + kwt(27);
    
}
- (void)qbBtnClick{
    self.model.status = -1;
    [self btnClickWithbtn:self.qbBtn];
    [self refreshingData];
    
}
- (void)zjBtnClick{
    self.model.status = 0;
    [self btnClickWithbtn:self.zjBtn];
    [self refreshingData];
    
}
- (void)jsBtnClick{
    self.model.status = 1;
    [self btnClickWithbtn:self.jsBtn];
    [self refreshingData];
    
}

-(void)refreshingData{
    self.pageNo = 1;
    [self.modelArray removeAllObjects];
    [self loadNewData];
}

- (void)loadNewData {
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [self.model GJDict];
    [dict setObject:@(self.pageNo) forKey:@"pageNo"];
    [dict setObject:@(self.pageSize) forKey:@"pageSize"];
    [dict setObject:@(self.tissueType) forKey:@"tissueType"];
    [dict setObject:self.tissueId forKey:@"tissueId"];
    
    //    if(![self.dict.allKeys containsObject:@"startTime"]){///没有开始时间
    //        [self.dict setObject:self.createTime forKey:@"createTime"];
    //    }
    
    [SCNetManger PostAlarmManageGetAlarmManageListWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * array = [NSArray yy_modelArrayWithClass:WarnModel.class json:model.result[@"records"]];
            [self.modelArray addObjectsFromArray:array];
            if(array.count == 0 && self.modelArray.count > 0){
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshing];
            }
            [self.tableview.mj_header endRefreshing];
            [self.tableview reloadData];
        } else {
            [self.view showWarning:model.message];
        }
    }];
    NSMutableDictionary * gjdict = [NSMutableDictionary dictionary];
    [gjdict setObject:@(self.tissueType) forKey:@"tissueType"];
    [gjdict setObject:self.tissueId forKey:@"tissueId"];
    if(self.model.alarmDegree != -1){///-1是全部
        [gjdict setObject:@(self.model.alarmDegree) forKey:@"alarmDegree"];
    }
    [SCNetManger PostAlarmManageGetAlarmPendSumWithDict:gjdict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        if(model.code == 200){
            NSString * count = [NSString stringWithFormat:@"%@", [model.result objectForKey:@"sum"]];
            NSLog(@"数量%@",count);
            if(count.intValue > 0){
                self.sumLab.hidden = NO;
                self.sumLab.text = count;
                if(count.intValue > 99){
                    self.sumLab.text = @"99+";
                }
            } else {
                self.sumLab.hidden = YES;
            }
        } else {
            [self.view showWarning:model.message];
        }
    }];
    
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView * headerview = [UIView new];
   
    return headerview;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kwt(10);
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.modelArray[indexPath.row].CellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    
    WarnListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"WarnListTableViewCell"];
    if (cell == nil) {
        cell = [[WarnListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WarnListTableViewCell"];
    }
    if(self.modelArray.count > indexPath.row){
        cell.model = self.modelArray[indexPath.row];

    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell ;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WarnDetailsViewController * vc = [WarnDetailsViewController new];
    vc.ID = self.modelArray[indexPath.row].ID;
    [self.navigationController pushViewController:vc animated:YES];
}


@end

